﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using System.Data;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.Configuration.Statements;
using IBatisNet.Common;
using IBatisNet.DataMapper.Scope;
using System.Data.Common;
namespace Persistence
{
    public partial class TestSql
    {
        #region getSql
        /// <summary>
        /// 测试用的,生成的sql 不准确,不能执行
        /// </summary>
        /// <param name="statementName"></param>
        /// <param name="paramObject"></param>
        /// <returns></returns>
        public static string GetIbatisSql(string statementName, object paramObject)
        {
            try
            {
                ISqlMapper mapper = BaseMap.Mapper;
                IMappedStatement statement = mapper.GetMappedStatement(statementName);
                if (!mapper.IsSessionStarted)
                {
                    mapper.OpenConnection();
                }
                RequestScope scope = statement.Statement.Sql.GetRequestScope(statement, paramObject, mapper.LocalSession);
                statement.PreparedCommand.Create(scope, mapper.LocalSession, statement.Statement, paramObject);
                string strSql = scope.PreparedStatement.PreparedSql;
                foreach (IDataParameter pa in scope.IDbCommand.Parameters)
                {
                    IDbDataParameter para = mapper.LocalSession.CreateDataParameter();
                    strSql = strSql.Replace(pa.ParameterName, pa.Value.ToString());
                }
                return strSql;
            }
            catch (Exception error)
            {
                throw new Exception("打印ibatis sql语句出错:" + error.Message);
            }
        }
        #endregion
    }
}
